/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using EvaluationException = System.Qizx.Api.EvaluationException;
using QName = System.Qizx.Api.QName;
using DateTimeException = System.Qizx.Api.Util.XQDateTimeException;
using Duration = System.Qizx.Api.Util.XQDuration;
using NamespaceContext = System.Qizx.Util.NamespaceContext;
using IQName = System.Qizx.Xdm.IQName;
using EvalContext = System.Qizx.XQuery.EvalContext;
using XQItem = System.Qizx.XQuery.XQItem;
using XQItemType = System.Qizx.XQuery.XQItemType;
using XQType = System.Qizx.XQuery.XQType;
using XQValue = System.Qizx.XQuery.XQValue;
namespace System.Qizx.XQuery.Dt
{
	
	public class DayTimeDurationType:DurationType
	{
		public override QName Name
		{
			get
			{
				return IQName.GetQName(NamespaceContext.XSD, ShortName);
			}
			
		}
		public override string ShortName
		{
			get
			{
				return "dayTimeDuration";
			}
			
		}
		
		public override string ToString()
		{
			return "xs:dayTimeDuration";
		}
		
		public override int quickCode()
		{
			return QT_DTDUR;
		}
		
		public override XQValue cast(XQItem value_Renamed, EvalContext context)
		{
			XQItemType type = value_Renamed.ItemType;
			try
			{
				
				switch (type.quickCode())
				{
					
					case XQType.QT_STRING: 
					case XQType.QT_UNTYPED: 
						return SingleDuration.newDT(Duration.parseDuration(value_Renamed.ValueAsString.Trim()));
					
					case XQType.QT_DUR: 
					case XQType.QT_YMDUR: 
						double s = value_Renamed.Duration.TotalSeconds;
						return SingleDuration.newDT(Duration.newDayTime(s));
					
					case XQType.QT_DTDUR: 
						return SingleDuration.newDT(value_Renamed.Duration.copy());
					
					case XQType.QT_INT: 
					case XQType.QT_DEC: 
					case XQType.QT_FLOAT: 
					case XQType.QT_DOUBLE:  // extension
						if (context != null && !context.sObs())
							return SingleDuration.newDT(new Duration(0, value_Renamed.ValueAsDouble));
						// FALL THROUGH:
						goto default;
					
					default: 
						invalidCast(type);
						break;
					
				}
			}
			catch (DateTimeException e)
			{
				castException(e);
			}
			return null; // dummy
		}
	}
}